﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;

namespace final_darskade.Models
{
    public class LogInModel
    {
        private darskadeEntities darskadeContext = new darskadeEntities();
        public string username { get; set; }
        public string password { get; set; }
        public string security_question { get; set; }
        public string security_answer { get; set; }
        public string new_password { get; set; }
        public string confirm_new_password { get; set; }
        //check value of username and password are empty or null?
        public bool ValidateUser()
        {
            
            
            if (String.IsNullOrEmpty(username)) throw new ArgumentException("Value cannot be null or empty.", "userName");
            if (String.IsNullOrEmpty(password)) throw new ArgumentException("Value cannot be null or empty.", "password");

            return IsAuthenticated();
        }

        //checking authentication
        public bool IsAuthenticated()
        {
            //darskadeContext.Connection.Open();

            user checkingUser = darskadeContext.users.SingleOrDefault(u => u.username == username);
            if (checkingUser == null)
            {
                // the user does not exist
                return false;
            }
            else
            {
                string hashedInsertedPass = Helper.getMd5Hash(password);
                if (checkingUser.pass == hashedInsertedPass)
                {
                    return true;
                }
                else
                {
                    // the pass is wrong
                    return false;
                }
            }
        }

        //checking authorization
        //we want to use roles to do authorization
        public string Authorization()
        {
            var query = from user in Helper.database.users
                        where user.username == username
                        select user;
            if (query.Any())
            {
                string rolename = query.First().role.role_name;
                return rolename;
            }
            else
                return null;
        }

        //checking first time login of user by checking value of "is_first_login" field in user table in database
        public bool FirstLogin()
        {
            user user_check = darskadeContext.users.SingleOrDefault(x => x.username == username);
            return user_check.is_first_login;
        }

        public string Forget_password()
        {
            user userChekning = darskadeContext.users.SingleOrDefault(x => x.username == username);
            if (string.IsNullOrEmpty(username)) throw new ArgumentException("Value cannor be null or empty.", "username");
            if (String.IsNullOrEmpty(security_question)) throw new ArgumentException("Value cannot be null or empty.", "sequrity_question");
            if (String.IsNullOrEmpty(security_answer)) throw new ArgumentException("Value cannot be null or empty.", "sequrity_answer");

            if ((userChekning.security_question == security_question) && (userChekning.security_answer == security_answer))
            {
                return userChekning.pass;
            }
            else
            {
                return "Wrong!Please try again!";
            }
        }

        public string get_home_page()
        {
            return "~/Account";
        }

    }

    public class FirstLogin
    {
        private darskadeEntities darskadeContext = new darskadeEntities();

        private darskadeViewsEntities viewsEntities = new darskadeViewsEntities();
        //private darskadeEntities tablesEntities = new darskadeEntities();

        public string username { get; set; }
        [Required(ErrorMessage = "Please Enter Your Password.")]

        public string password { get; set; }
        [Required(ErrorMessage = "Please Enter your Current Password.")]

        public string fold_pass { get; set; }
        [Required(ErrorMessage = "Security Question required.")]
        public string security_question { get; set; }
        [Required(ErrorMessage = "Security Answer required.")]
        public string security_answer { get; set; }
        [Required(ErrorMessage = "Please Enter New Password .")]
        [StringLength(50, MinimumLength = 6, ErrorMessage = "The length of New Password must be greater than 6 .")]
        public string new_password { get; set; }
        [Required(ErrorMessage = "Confirm new password required.")]

        public string confirm_new_password { get; set; }

    }
   
}